<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

function rendsearchfield($item, $field_id) {
	$db = JFactory::getDBO();
	$query = "SELECT * FROM #__ja_form_fields WHERE id=$field_id";
	$db->setQuery($query);
	$field = $db->loadObject();
	if ($item->display_mode==1)
		$r = rendfield($item,$field);
	else 
	{
		$r = call_user_func($item->display_function);
	}
	
	return $r;
}


	function getMultiLevelOptions($table_name)
	{
		$db = JFactory::getDBO();
    	$query = "SELECT * FROM #__$table_name ";         
        $db->setQuery($query);
        $objects = $db->loadObjectList();
        $objectHTML = array();
        
        $objectHTML[] = JHTML::_('select.option',  "", JText::_("Please select")."...");
        
    	$children = array();	
    	if ($objects)
        {
        	foreach ($objects as $v )
			{
				$pt = $v->parent_id;			
				$list = @$children[$pt] ? $children[$pt] : array();
				array_push( $list, $v );
				$children[$pt] = $list;
			}
			if (is_array($children))
			{
				
				foreach ($children as $key=>$value)
				{
					$temp = array();
					$tmp_children = array();
					if (is_array($children[$key]))
					{
						foreach ($children[$key] as $k=>$v)
							$temp[$k] = $v->name;
						asort($temp);	
						foreach ($temp as $k=>$v)
							$tmp_children[$k] = $children[$key][$k];
						$children[$key] = $tmp_children;
					}
				}
				
			}
			$list = treerecurse(0, '', array(), $children );
			if($list){
				foreach ($list as $i => $item) {
					$treename = $item->treename;			
					$treename = JFilterOutput::ampReplace($treename);
					$treename = str_replace('"', '&quot;', $treename);
					if($item->id == 0)
						$list[$i]->treename = $treename;
					else 	
						$list[$i]->treename = $treename;
				}
			}
			$objects = $list;
        }
        
//        foreach ($objects as $key=>$value){
//            $objectHTML[] = JHTML::_('select.option',  $key, $value->treename);
//        }
        return $objects;		
	}


function getLocationCombo($selected_value='')
	{
		$r = '<div style="max-height: 200px;overflow: scroll !important;width:270px;">';
		$locationOptions = getMultiLevelOptions('ja_location');
		initialLocation_JSVar($locationOptions);
//		$r = JHTML::_('select.genericlist',   $locationOptions, 'location[]',  "size='10' multiple style=\"width:250px;\"", 'value', 'text', $selected_value);		
		foreach ($locationOptions as $key=>$value)
		{
//			$r .= $value->pretext;
			if (is_array($selected_value))
			{
				if (!in_array($value->id,$selected_value))
					$r .= '<input onclick="checkbox_clicked(this,\'location\',0)" id="location_'.$value->id.'" type="checkbox" name="location[]" value="'.$key.'" />'.$value->treename.'<br />';
			
				else 
					$r .= '<input checked onclick="checkbox_clicked(this,\'location\',0)" id="location_'.$value->id.'" type="checkbox" name="location[]" value="'.$key.'" />'.$value->treename.'<br />';
			}
			else 
				$r .= '<input onclick="checkbox_clicked(this,\'location\',0)" id="location_'.$value->id.'" type="checkbox" name="location[]" value="'.$key.'" />'.$value->treename.'<br />';
		}
		$r .= "</div>";
		return $r;
	}

function getCategoryCombo($selected_value)
	{
		$r = '<div style="max-height: 200px;overflow: scroll !important;width:270px;">';
		$categoryOptions = getMultiLevelOptions('ja_categories');
		initialCat_JSVar($categoryOptions);
//		$r = JHTML::_('select.genericlist',   $categoryOptions, 'category[]',  "size='10' multiple style=\"width:250px;\"", 'value', 'text', $selected_value);
		foreach ($categoryOptions as $key=>$value)
		{
//			$r .= $value->pretext;
			if (!in_array($value->id,$selected_value))
				$r .= '<input onclick="checkbox_clicked(this,\'category\',0)" id="category_'.$value->id.'" type="checkbox" name="category[]" value="'.$key.'" />'.$value->treename.'<br />';
			else 	
				$r .= '<input checked onclick="checkbox_clicked(this,\'category\',0)" id="category_'.$value->id.'" type="checkbox" name="category[]" value="'.$key.'" />'.$value->treename.'<br />';
		}
		$r .= "</div>";
		return $r;
	}

function initialCat_JSVar($arr_category)
	{
		echo '<script type="text/javascript" language="javascript">';
		echo '/* <![CDATA[ */';
		echo 'child_category = new Array();';
		foreach ($arr_category as $key=>$value)
		{
			echo 'child_category['.$value->id.']=new Array();';
		}
		foreach ($arr_category as $key=>$value)
			if ($value->parent_id!=0)
			{
				echo 'temp_len = child_category['.$value->parent_id.'].length;';
				echo 'child_category['.$value->parent_id.'][temp_len]='.$value->id.';';
			}
		echo '/* ]]> */';
		echo '</script>';
	}
	
	function initialLocation_JSVar($arr_location)
	{
		echo '<script type="text/javascript" language="javascript">';
		echo '/* <![CDATA[ */';
		echo 'child_location = new Array();';
		foreach ($arr_location as $key=>$value)
		{
			echo 'child_location['.$value->id.']=new Array();';
		}
		foreach ($arr_location as $key=>$value)
			if ($value->parent_id!=0)
			{
				echo 'temp_len = child_location['.$value->parent_id.'].length;';
				echo 'child_location['.$value->parent_id.'][temp_len]='.$value->id.';';
			}
		echo '/* ]]> */';
		echo '</script>';
	}
	


?>
<script type="text/javascript" language="javascript">
//<![CDATA[
function checkbox_clicked(obj,checkbox_type,index)
{
	if (checkbox_type=='category')
		for (i=index;i<child_category[obj.value].length;i++)
		{
			ele = document.getElementById(checkbox_type+'_'+child_category[obj.value][i]);
			ele.checked = obj.checked;
			if (child_category[ele.value].length>0)
			{
				temp = i;
				checkbox_clicked(ele,checkbox_type,0);
				i = temp;
			}
		}
	else
		for (i=index;i<child_location[obj.value].length;i++)
		{
			ele = document.getElementById(checkbox_type+'_'+child_location[obj.value][i]);
			ele.checked = obj.checked;
			if (child_location[ele.value].length>0) 
			{
				temp = i;
				checkbox_clicked(ele,checkbox_type,0);
				i = temp;
			}
		}
		return index;
}
//]]>
</script>
